import requests
from bs4 import BeautifulSoup
import threading
import time

def get_page_content(url):
    try:
        response = requests.get(url)
        soup = BeautifulSoup(response.text, 'html.parser')
        start_time = soup.select_one('div.label:contains("起刊时间")')
        subscribe_year = soup.select_one('div.label:contains("订阅年份")')
        
        if start_time and subscribe_year:
            return start_time.get_text(), subscribe_year.get_text()
        else:
            return None
    except:
        return None


def worker(start, end):
    for i in range(start, end):
        url = f'https://www.maigeyin.com/a/{i}.html'
        result = get_page_content(url)
        if result is None:
            with open('log.txt', 'a') as f:
                f.write(f'{url}\n')
        else:
            print(f'{url}: 起刊时间 - {result[0]}, 订阅年份 - {result[1]}')


def main():
    start_time = time.time()
    threads = []
    step = (17520 - 1) // 30 + 1
    for i in range(1, 17521, step):
        t = threading.Thread(target=worker, args=(i, i+step))
        threads.append(t)
        t.start()
        
    for t in threads:
        t.join()
        
    end_time = time.time()
    print(f'总耗时: {end_time - start_time:.2f}秒')

if __name__ == '__main__':
    main()
